jQuery.fn.extend({
    decorateTable: function()
    {
        jQuery(this).find("tbody tr:not('.quick-form'):even td").css({
            "background-color": "#f3f3f3",
            "cursor":"pointer"
        });
        this.actionWhenSelect();
        return jQuery(this);
    },
    actionWhenSelect: function() {
        var formSubmit = jQuery("form");
        if (!jQuery(formSubmit)) {
            return;
        }
        jQuery(formSubmit).find("select:not('.massSelectAction')").change(function() {
            jQuery(formSubmit).submit();
        });
    },
    lightHoverRow: function()
    {
        jQuery(this).find("tbody tr").hover(function(){
            jQuery(this).find(".quick-editor").css({
                'visibility':'visible'
            });
        }, function() {
            jQuery(this).find(".quick-editor").css({
                'visibility':'hidden'
            });
        });
        return jQuery(this);
    },
    activePageIndex: function(pageIndexValueClassName, pageIndexHolderID, mainFormID) {
        jQuery("." + pageIndexValueClassName).click(function() {
            var pageIndex = jQuery.trim(jQuery(this).html());
            jQuery("#" + pageIndexHolderID).val(pageIndex);
            jQuery("#" + mainFormID).submit();
            return false;
        });
        return jQuery(this);
    },
    activeEditRow: function()
    {
        jQuery(this).find("tr.activeRow").each(function() {
            var url = jQuery(this).find("input.editUrl").val();
            jQuery(this).find("td:not('.noActiveRow')").click(function() {
                window.location=url;
            });
        });
        return jQuery(this);
    },
    activeSortableAbility: function(formId, classColumnFilter, prefixColumnId, inputOrderId, inputOrderDirectionId)
    {
        var form = jQuery("#" + formId);
        jQuery("." + classColumnFilter).click(function() {
            var id = jQuery(this).attr("id").replace(prefixColumnId, "");
            jQuery("#" + inputOrderId).attr("value", id);
            var value = jQuery("#" + inputOrderDirectionId).attr("value");
            if (value == "desc") {
                jQuery("#" + inputOrderDirectionId).attr("value", "asc");
            } else {
                jQuery("#" + inputOrderDirectionId).attr("value", "desc");
            }
            // set current sort
            setCookie(formId + '_sort_name', id, null);
            setCookie(formId + '_sort_direction', jQuery("#" + inputOrderDirectionId).attr("value"), null);
            form.submit();
        });
        var sortedColumnId = prefixColumnId + getCookie(formId + '_sort_name');
        var direction = getCookie(formId + '_sort_direction');
        jQuery("#" + sortedColumnId).toggleClass("sort" + direction.toUpperCase());
        return jQuery(this);
    },
    activeResetButton: function(formId, btnClass, resetUrl) {
    	jQuery("." + btnClass).click(function() {
    		// Reset Sorted Column
    		setCookie(formId + "_sort_name", "", null);
        	setCookie(formId + "_sort_direction", "", null);
    		window.location = resetUrl;
    	});    	
    	return jQuery(this);
    },
    checkboxCallbackHandler: function(checkboxClass, inputStoreId, itemSelectedId, hiddenSelectedId)
    {
        var checkboxes = jQuery("." + checkboxClass);
        if (!jQuery(checkboxes).exists()) {
            return;
        }
        var inputStoreValue;
        jQuery(checkboxes).each(function() {
            jQuery(this).click(function() {
                var checked = jQuery(this).attr("checked");
                var checkboxValue = jQuery(this).attr("value");
                inputStoreValue = toggleCheckedValue(checked, checkboxValue, jQuery("#" + inputStoreId).attr("value"));
                jQuery("#" + inputStoreId).attr("value", inputStoreValue);
            });
        });
        return jQuery(this);
    },
    radioCallbackHandler: function(radioClass, inputStoreId, url, callbackHandler)
    {
        var radios = jQuery("." + radioClass);
        if (!jQuery(radios).exists()) {
            return;
        }
        jQuery(radios).click(function() {
            var checkedValue = jQuery(this).attr("value");
            jQuery("#" + inputStoreId).attr("value", checkedValue);
            var data = "value=" + checkedValue;
            jQuery.ajax({
                type: 'POST',
                url: url,
                data: data,
                success: callbackHandler
            });
        });
        return jQuery(this);
    },
    activeAjaxForm: function(gridFormId, containerId)
    {
        var gridForm = jQuery("#" + gridFormId);
        var container = jQuery("#" + containerId);
        jQuery(gridForm).submit(function() {
            var url = jQuery(this).attr("action");
            var data = jQuery(this).serialize();
            jQuery.ajax({
                type: 'POST',
                url: url,
                data: data,
                success: function(data) {
                    jQuery(container).html(data);
                }
            });
            return false;
        })
        return jQuery(this);
    },
    activeEventChange: function(inputClass, gridFormId) {
        jQuery("." + inputClass).change(function() {
            jQuery("#" + gridFormId).submit();
        });
        return jQuery(this);
    },
    editor: function(baseDir) {
        CKEDITOR.replace(jQuery(this).attr('id'),{
            filebrowserBrowseUrl : baseDir + 'ckfinder.html',
            filebrowserImageBrowseUrl : baseDir + 'ckfinder.html?type=Images',
            filebrowserFlashBrowseUrl : baseDir + 'ckfinder.html?type=Flash',
            filebrowserUploadUrl :
            baseDir + 'core/connector/php/connector.php?command=QuickUpload&type=Files&currentFolder=/archive/',
            filebrowserImageUploadUrl :
            baseDir + 'core/connector/php/connector.php?command=QuickUpload&type=Images&currentFolder=/cars/',
            filebrowserFlashUploadUrl : baseDir + 'core/connector/php/connector.php?command=QuickUpload&type=Flash'
        }
        );
    },
    activeColorBox: function(className, options) {
    	jQuery("." + className).colorbox(options);
    	return jQuery(this);
    },
    activeFancyBox: function(className, options) {
    	jQuery("." + className).fancybox(options);
    	return jQuery(this);
    }
    /*,
    quickEditor: function(deleteUrl, paramName) {
        jQuery(this).find("tbody .btn-delete").click(function() {
            var row = jQuery(this).parent().parent().parent();
            jQuery(row).find("td").effect("highlight", {
                'color': 'red',
                'mode':'show'
            }, 500);
            var entityId = row.attr("id").replace("row_", "");
            deleteUrl += "?" + paramName + "=" + entityId;
            jQuery.ajax({
                type: 'POST',
                url: deleteUrl,
                success: function(data) {
                    var frmRow = jQuery(row).next("tr.quick-form");
                    jQuery(row).fadeOut();
                    jQuery(frmRow).fadeOut();
                }
            });
        // Ajax effect
        });
        jQuery(this).find("tbody .btn-edit").click(function() {
            var row = jQuery(this).parent().parent().parent();
            var frmRow = jQuery(row).next("tr.quick-form");
            jQuery(frmRow).fadeIn();
            jQuery(frmRow).find(".close-form").click(function() {
                jQuery(frmRow).hide();
            });
        });
    }
    */
});
// Using For Paginating Gridview
function prevPage(inputPageIndexId, formSubmitId) 
{
    var input = jQuery("#" + inputPageIndexId);
    if (!input) {
        return;
    }
    var inputValue = 1*(input.val());
    inputValue--;
    if (inputValue <= 0) {
        inputValue = 1;
    }
    jQuery("#" + inputPageIndexId).val(inputValue);
    var formSubmit = jQuery("#" + formSubmitId);
    if (!jQuery(formSubmit)) {
        return;
    }
    jQuery(formSubmit).submit();
}
function nextPage(inputPageIndexId, formSubmitId, maxAllowValue)
{
    var input = jQuery("#" + inputPageIndexId);
    if (!input.exists()) {
        return;
    }
    var inputValue = 1*(input.val());
    inputValue++;
    if (inputValue >= maxAllowValue) {
        inputValue = maxAllowValue;
    }
    jQuery("#" + inputPageIndexId).val(inputValue);
    var formSubmit = jQuery("#" + formSubmitId);
    if (!jQuery(formSubmit).exists()) {
        return;
    }
    jQuery(formSubmit).submit();
}
// Active Mass Action
function activeMassAction(formId, massSelectionId)
{
    var form = jQuery("#" + formId);
    var selectAction = jQuery("#" + massSelectionId);
    if (!jQuery(form.exists()) || !jQuery(selectAction).exists()) {
        return;
    }
    var url = jQuery(selectAction).val();
    if (url.length == 0) {
        return;
    }
    jQuery(form).attr("action", url).submit();
}
// Mass Select
function selectAll(checkboxClass, inputStoreId, url, itemSelectedId, hiddenSelectedId)
{
    jQuery("." + checkboxClass).each(function() {
        jQuery(this).attr("checked", "checked");
    });
    jQuery("#loading").fadeIn('slow');
    jQuery.ajax({
        url: url,
        success: function(data) {
            jQuery('#' + inputStoreId).val(data);
            jQuery("#loading").fadeOut('slow');
        }
    });
    return false;
}
// Mass Un Select
function unSelectAll(checkboxClass, inputStoreId, itemSelectedId, hiddenSelectedId)
{
    jQuery("#" + inputStoreId).attr("value", "");
    jQuery("." + checkboxClass).each(function() {
        jQuery(this).attr("checked", "");
    });
    return false;
}
// Mass Select Visible
function selectVisible(checkboxClass, inputStoreId, itemSelectedId, hiddenSelectedId)
{
    var inputStoreValue;
    jQuery("." + checkboxClass).each(function() {
        jQuery(this).attr("checked", "checked");
        var checkboxValue = jQuery(this).attr("value");
        inputStoreValue = jQuery("#" + inputStoreId).attr("value").toString().replace("," + checkboxValue + ",", "");
        inputStoreValue += "," + checkboxValue + ",";
        jQuery("#" + inputStoreId).attr("value", inputStoreValue);
    });
    return false;
}
// Mass Un Select Visible
function unSelectVisible(checkboxClass, inputStoreId, itemSelectedId, hiddenSelectedId)
{
    var inputStoreValue;
    jQuery("." + checkboxClass).each(function() {
        jQuery(this).attr("checked", "");
        var checkboxValue = jQuery(this).attr("value");
        inputStoreValue = jQuery("#" + inputStoreId).attr("value").toString().replace("," + checkboxValue + ",", "");
        jQuery("#" + inputStoreId).attr("value", inputStoreValue);
    });
    return false;
}
function resetAjaxForm(gridFormId, containerId)
{	
    var form = jQuery("#" + gridFormId);
    var container = jQuery("#" + containerId);
    if (!jQuery(form).exists() || !jQuery(container).exists()) {
        return false;
    }
    var url = jQuery(form).attr("action");
    var data = "reset=1";
    jQuery.ajax({
        type: 'POST',
        data: data,
        url: url,
        success: function(data) {
            jQuery(container).html(data);
        }
    });
}
function toggleCheckedValue(checked, value, inputValue)
{
    if (checked) {
        inputValue += "," + value + ",";
    } else {
        inputValue = inputValue.replace("," + value + ",", "");
    }
    return inputValue;
}
function confirmDelete(itemName) {
	if (confirm("Do you want to delete this " + itemName)) {
		return true;
	}
	return false;
}
